#include "experimentpropertyui.h"
#include "ui_experimentpropertyui.h"

ExperimentPropertyUi::ExperimentPropertyUi(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ExperimentPropertyUi)
{
    ui->setupUi(this);

    /* 使 QComboBox 的 下拉框 Item 样式生效 */
    ui->sComputer->setView(new QListView());
    ui->expType->setView(new QListView());

    /* 限制文本框输入限制 */
    /* 实验名称 */
    QRegExp expNamerx("(\\w|-|\\s){1,50}");
    QRegExpValidator *expNameReg = new QRegExpValidator(expNamerx, this);
    ui->expName->setValidator(expNameReg);
    /* 用户名称 */
    QRegExp userNamerx(".{1,50}");
    QRegExpValidator *userNameReg = new QRegExpValidator(userNamerx, this);
    ui->userName->setValidator(userNameReg);
    /* 编码 */
    QRegExp codingrx(".{1,40}");
    QRegExpValidator *codingReg = new QRegExpValidator(codingrx, this);
    ui->coding->setValidator(codingReg);

    /* 备注文本框滚动条大小 */
    ui->expComment->verticalScrollBar()->setStyleSheet("QScrollBar:vertical { width: 20px; }");

    /* 对象实例化 */
    scmW = new SlaveComputerManagement();
    configFileService = new ConfigFileService();
    devComboboxEdit = new CustomLineEdit();

    /* 设置QLineEdit样式 */
    devComboboxEdit->setStyleSheet("QLineEdit{"
                                   "font-size: 18px;"
                                   "font-family: 'Alibaba PuHuiTi';"
                                   "background: transparent;"
                                   "}");

    /* 改变devlineedit的鼠标悬停样式 */
    devComboboxEdit->setCursor(Qt::PointingHandCursor);

    /* 获取当前日期时间 */
    expTime = QDateTime::currentDateTime();
    /* 转QString */
    expStringDate = expTime.toString("yyyy-MM-dd hhmmss");

    /* 从配置文件中读取实验类型 */
    experimentType = configFileService->getExperimentType();

    /* 渲染实验类型 */
    for(int i = 0; i < experimentType.size(); i++){
        ui->expType->addItem(experimentType[i]);
    }

    /* 实验属性配置 */
    ui->expName->setText(expStringDate);
    ui->userName->setPlaceholderText("请输入用户名");
    ui->coding->setPlaceholderText("请输入编码");
    ui->expComment->setPlaceholderText("请输入实验备注信息");
    /* 存入 experiment 结构体中 */
    experiment.experiment_name = expStringDate;
    experiment.type = ui->expType->currentText();
    experiment.reaction_volume = "25";  // 反应体积默认25ul

    /* 设置设备列表的QLineEdit */
    ui->sComputer->setLineEdit(devComboboxEdit);

    /* 不可聚焦 */
    devComboboxEdit->setFocusPolicy(Qt::NoFocus);

    /* 创建实验 */
    /* 改变实验名称 */
    connect(ui->expName, &QLineEdit::textChanged, this, [=](){
        experiment.experiment_name = ui->expName->displayText();
        emit experimentInfoChanged(experiment);
    });
    /* 输入用户名 */
    connect(ui->userName, &QLineEdit::editingFinished, this, [=](){
        experiment.user_name = ui->userName->displayText();
        emit experimentInfoChanged(experiment);
    });
    /* 输入编码 */
    connect(ui->coding, &QLineEdit::editingFinished, this, [=](){
        experiment.coding = ui->coding->displayText();
        emit experimentInfoChanged(experiment);
    });
    /* 改变实验类型 */
    connect(ui->expType, &QComboBox::currentTextChanged, this, [=](QString expType){
        experiment.type = expType;
        emit experimentTypeChanged(experiment);
    });
    /* 输入实验备注 */
    connect(ui->expComment, &QTextEdit::textChanged, this, [=](){
        if(ui->expComment->toPlainText().size() > 500) {
            /* 获取当前光标 */
            QTextCursor cursor = ui->expComment->textCursor();
            /* 获取当前光标位置 */
            int position = ui->expComment->textCursor().position();
            /* 限制文本框值 */
            ui->expComment->setPlainText(ui->expComment->toPlainText().mid(0, 500));
            /* 设置文本框光标位置 */
            cursor.setPosition(500, QTextCursor::MoveAnchor);
            ui->expComment->setTextCursor(cursor);
        }
        experiment.experiment_remarks = ui->expComment->toPlainText();
        emit experimentInfoChanged(experiment);
    });

    /* 点击设备combobox的lineedit，也弹出下拉框 */
    connect(devComboboxEdit, &CustomLineEdit::editClick, this, [=](){
        /* 弹出下拉菜单 */
        ui->sComputer->showPopup();
    });

    /* 下一步按钮 */
    connect(ui->nextBtn, &QPushButton::clicked, this, &ExperimentPropertyUi::nextCallback);

    /* 保存按钮 */
    connect(ui->saveBtn, &QPushButton::clicked, this, [=](){
        emit ExperimentPropertyUi::saveEvent();
    });

}

ExperimentPropertyUi::~ExperimentPropertyUi()
{
    delete ui;
}

/* 下一步 */
void ExperimentPropertyUi::nextCallback(){
    /* 发送实验属性数据 */
    emit ExperimentPropertyUi::nextEvent();
}

/* 返回数据信息 */
Experiment ExperimentPropertyUi::returnPageInfos(){
    return this->experiment;
}

/* 渲染 wlk 的实验属性 */
void ExperimentPropertyUi::renderWLKProperty(Experiment experiment, ExperimentService *expService){
    /* 写入反应体积 */
    experiment.reaction_volume = "25";

    /* 写入实验属性 */
    this->experiment = experiment;

    /* 写入实验信息数据 */
    this->expService = expService;

    /* 阻断信号发射 */
    ui->expName->blockSignals(true);
    ui->userName->blockSignals(true);
    ui->coding->blockSignals(true);
    ui->expType->blockSignals(true);
    ui->expComment->blockSignals(true);

    /* ui界面写入 */
    ui->expName->setText(experiment.experiment_name);
    ui->userName->setText(experiment.user_name);
    ui->coding->setText(experiment.coding);
    ui->expType->setCurrentText(experiment.type);
    ui->expComment->setText(experiment.experiment_remarks);
    /* 同步页面名称 */
    emit experimentInfoChanged(experiment);

    /* 解除阻断信号发射 */
    ui->expName->blockSignals(false);
    ui->userName->blockSignals(false);
    ui->coding->blockSignals(false);
    ui->expType->blockSignals(false);
    ui->expComment->blockSignals(false);

    /* 更新数据库实验信息 */
    expService->update(experiment);
}

/* 反写实验id */
void ExperimentPropertyUi::writeBackExperimentId(QString id){
    this->experiment.experiment_id = id;
}

/* 发送实验属性 */
Experiment ExperimentPropertyUi::sendExperimentProperty(){
    return this->experiment;
}

/* 发送实验名称 */
QString ExperimentPropertyUi::sendExperimentName(){
    return this->experiment.experiment_name;
}

/* 所有组件失效 */
void ExperimentPropertyUi::allComponentsInvalid(){
    LOG << "属性组件失效";

    ui->expName->setReadOnly(true);
    ui->userName->setReadOnly(true);
    ui->coding->setReadOnly(true);
    ui->expComment->setReadOnly(true);
    ui->sComputer->setEnabled(false);
    ui->expType->setEnabled(false);

    LOG << "<<<<<<<<<<<<<<属性组件失效";
}

/* 所有组件生效 */
void ExperimentPropertyUi::allComponentsValid(){
    ui->expName->setReadOnly(false);
    ui->userName->setReadOnly(false);
    ui->coding->setReadOnly(false);
    ui->expComment->setReadOnly(false);
    ui->sComputer->setEnabled(true);
    ui->expType->setEnabled(true);
}

/* 鼠标聚焦实验名称文字框 2023.6.2*/
void ExperimentPropertyUi::setFocusToExpNameLineEdit()
{
    if(ui->expName->isVisible()){
        ui->expName->setFocus(); // 设置组件聚焦
    }

}

/* 窗口显示函数 */
void ExperimentPropertyUi::showEvent(QShowEvent *event){
    this->setFocusToExpNameLineEdit(); // 光标聚焦
}


